package com.example.service.impl;

import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.example.dao.LinesDao;
import com.example.dao.StationDao;
import com.example.domain.Lines;
import com.example.domain.Station;
import com.example.service.LinesService;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@Service
public class LinesServiceImpl implements LinesService {

	@Autowired
	private LinesDao linesDao;
	
	@Autowired
	private StationDao stationDao;

	@Override
	public Lines findByLineId(Integer lineId) {
		return linesDao.findByLineId(lineId);
	}

	@Override
	public void setLinesInfo() {
		CloseableHttpClient httpClient = HttpClients.createDefault();
		try {
			String url = "http://cs.gzmtr.com/base/doLoadLines.do?callback=jQuery180044599126842203907_1529930602367&_=152993";
			HttpGet get = new HttpGet(url);
			CloseableHttpResponse response = httpClient.execute(get);
			HttpEntity entity = response.getEntity();
			if (null != entity) {
				String result = EntityUtils.toString(entity);
				result = result.substring(42, result.length() - 1);
				JSONObject json = JSONObject.fromObject(result);
				JSONArray array = json.getJSONArray("lines");
				for (int i = 0; i < array.size(); i++) {
					JSONObject obj = array.getJSONObject(i);
					Lines line = new Lines();
					line.setKey(obj.optString("key"));
					line.setLineName(obj.optString("lineName"));
					line.setLineId(obj.optInt("lineId"));
					line.setLineDisribe(obj.optString("lineDiscribe"));
					line.setGisKey(obj.optString("gisKey"));
					line.setColor(obj.optString("color"));
					linesDao.insert(line);
					JSONArray stations = obj.getJSONArray("stages");
					for(int j = 0; j < stations.size(); j++){
						JSONObject s = stations.getJSONObject(j);
						System.out.println("obj[" + j + "]:" + s);
						Station station = new Station();
						station.setLineId(obj.optInt("lineId"));
						station.setStageId(s.optInt("stageId"));
						station.setStageName(s.optString("stageName"));
						station.setStageKey(s.optString("stageKey"));
						station.setStageDisc(s.optString("stageDisc"));
						station.setStageseq(s.optInt("stageseq"));
						station.setGisKey(s.optString("gisKey"));
						station.setStageIs(s.optString("stageIs"));
						System.out.println("station[" + j + "]:" + station);
						stationDao.insert(station);
					}
					System.out.println(obj.optString("lineId") + ", lineName:" + obj.optString("lineName"));
				}
				// System.out.println("Response content:" + result);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				httpClient.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	@Override
	public List<Lines> findAllLines() {
		return linesDao.findAllLines();
	}

}
